<?php
namespace app\serveruser\service\user;

use \app\manage\service\ApiBase;

/**
 * 用户信息类
 * @author  李全春  <adv30@126.com>
 */
class User extends ApiBase
{
    protected $userValidate = '\app\serveruser\validate\User';
    protected $userLogic = '\app\serveruser\logic\User';

    // 用户登录
    public function login ()
    {
        $data = $this->haveIssetReceiveData(['id'=>'', 'name'=>'', 'phone'=>'', 'login_password'=>' ', 'token' => ' ', 'is_auto_login'=>0]);
        if ($data['name'] === '' && $data['phone'] === '' && $data['id'] === 0) {
            return verify_err(null, '用户名和手机号至少要输入一个');
        }
        return $this->stepRun($data, 'login', 'login');
    }

    //用户自动登录
    public function autoLogin(): array 
    {
        $data = $this->haveIssetReceiveData(['name'=>'', 'phone'=>'', 'token' => ' ', 'auto_login_time'=>0, 'auto_login_token'=>'']);
        if ($data['name'] === '' && $data['phone'] === '') {
            return verify_err(null, '用户名和手机号至少要输入一个');
        }
        return $this->stepRun($data, 'autoLogin', 'autoLogin');
    }

    /*自动登录超时后的处理*/
    public function autoLoginOvertime(): array 
    {
        $data = $this->haveIssetReceiveData(['name'=>' ']);
        return $this->stepRun($data, 'autoLoginOvertime');
    }

    // 用户注册
    public function register(): array
    {
        $data = $this->haveIssetReceiveData(['name'=>' ', 'nickname'=>'0', 'sex'=>'no', 'phone'=>'0', 'login_password'=>' ', 'other_info' => '{}']);
        if($data['other_info'] !== '{}'){ $data['other_info'] = json_encode($data['other_info']); }
        return $this->stepRun($data, 'register', 'register');
    }
    // 用户完善信息
    public function perfect(): array
    {
        $data = $this->haveIssetReceiveData(['name'=>'', 'id'=>'', 'phone'=>'', 'other_info' => '{}']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        } else if ($data['other_info'] === '{}'){
            return verify_err($data, 'other_info不能为空');
        } else if (is_array($data['other_info'])){
            $data['other_info'] = json_encode($data['other_info']);
        }
        return $this->stepRun($data, 'perfect', 'perfect');
    }
    // 用户审核通过
    public function passAudit(): array
    {
        $data = $this->haveIssetReceiveData(['name'=>'', 'id'=>'', 'phone'=>'']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->stepRun($data, 'passAudit', 'passAudit');
    }
    // 用户分页列表数据
    public function list(): array
    {
        $data = $this->haveIssetReceiveData(['page'=>1, 'list_rows'=>'', 'keywords'=>'', 'authen_status'=>''], []);
        return $this->stepRun($data, 'list', 'list');
    }
    // 更改用户信息(后台使用)
    public function edit(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'0', 'name'=>'', 'nickname'=>'', 'sex'=>'', 'phone'=>'',
            'row_ids'=>'','row_group_ids'=>'','other_info'=>'']);
        return $this->stepRun($data, 'edit', 'edit');
    }
    // 验证登录密码
    public function checkLoginPassword(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'', 'name'=>'', 'phone'=>'', 'login_password'=>' ']);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'checkLoginPassword', 'checkLoginPassword');
    }
    // 验证支付密码
    public function checkPayPassword(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'', 'name'=>'', 'phone'=>'', 'pay_password'=>' ']);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'checkPayPassword', 'checkPayPassword');
    }
    // 修改登录密码
    public function editLoginPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'old_login_password'=>' ', 'login_password'=>' ', 're_login_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'editLoginPassword', 'editLoginPassword');
    }
    // 修改支付密码
    public function editPayPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'old_pay_password'=>' ', 'pay_password'=>' ', 're_pay_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'editPayPassword', 'editPayPassword');
    }
    // 忘记登录密码-用手机验证码  # TODO 废弃
    public function forgetLoginPassword(): array
    {
        $setArr = ['name'=>' ', 'phone'=>' ', 'phone_code'=>' ', 'login_password'=>' ', 're_login_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        return $this->stepRun($data, 'forgetLoginPassword', 'forgetLoginPassword');
    }
    // 忘记支付密码 # TODO 废弃
    public function forgetPayPassword(): array
    {
        return auto_success(123);
    }
    // 重置用户登录密码-中间层验证手机验证码通过后再调用本方法
    public function resetLoginPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'login_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'resetLoginPassword', 'resetLoginPassword');
    }
    // 重置用户支付密码-中间层验证手机验证码通过后再调用本方法
    public function resetPayPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'pay_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'resetPayPassword', 'resetPayPassword');
    }
    // 根据tocken获取某一用户信息
    public function infoByTocken(): array
    {
        return auto_success(123);
    }
    // 根据 ID ||用户名 || 手机号 获取某一用户信息
    public function info(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'is_add_no_user_counts' => '', 'token'=>'']);
        if($data['is_add_no_user_counts'] == 1 && !$data['token']){
            return verify_err(null, '请传入参数token');
        }
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->stepRun($data, 'info', 'info');
    }
    // 根据 ID ||用户名 || 手机号 获取某一用户包含自动登录的用户全部信息
    public function infoAll(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->stepRun($data, 'infoAll');
    }
    // 初始化用户支付密码 在用户没有设置支付密码的情况下
    public function initPayPassword(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'login_password' => ' ', 'pay_password' => ' ']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->stepRun($data, 'initPayPassword', 'initPayPassword');
    }
    // 根据用户传入的IDS获取用户信息
    public function infoByIds(): array
    {
        $data = $this->haveIssetReceiveData(['ids' => '']);
        if('' === $data['ids']){
            return verify_err('请传入参数ids');
        }
        return $this->stepRun($data, 'infoByIds', 'infoByIds');
    }
    // 用户退出登录
    public function logout(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ']);
        return $this->stepRun($data, 'logout', 'logout');
    }
    // 给某一个用户某角色
    public function giveRoles(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'role_id'=>'0']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->stepRun($data, 'giveRoles', 'giveRoles');
    }
    // 移除某用户的某角色
    public function removeUserRole(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'role_id'=>'0']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->stepRun($data, 'removeUserRole', 'removeUserRole');
    }
    // 用户更改自己的信息
    public function editSelfInfo(): array
    {
        $data = $this->haveIssetReceiveData(['token' => ' ', 'nickname' => '', 'sex' => '', 'head_portrait'=>'']);
        if(array_eq($data, '', ['nickname', 'sex', 'head_portrait'])){
            return verify_err($data, 'nickname,sex,head_portrait至少要传一个');
        }
        return $this->stepRun($data, 'editSelfInfo', 'editSelfInfo');
    }
    // 用户更改自己的登录密码
	public function editSelfLoginPassword(): array
	{
        $data = $this->haveIssetReceiveData(['token' => ' ', 'login_password' => ' ', 'old_login_password' => ' ', 're_login_password'=>' ']);
        return $this->stepRun($data, 'editSelfLoginPassword', 'editSelfLoginPassword');
	}
    // 管理员重置普通用户登录密码
    public function resetUserLoginPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'login_password'=>' ', 're_login_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'resetUserLoginPassword', 'resetUserLoginPassword');
    }
    // 超级管理员重置所有用户密码的接口
    public function resetAllLoginPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'login_password'=>' ', 're_login_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'resetAllLoginPassword', 'resetAllLoginPassword');
    }
    // 用户更改自己的支付密码
    public function editSelfPayPassword(): array
    {
        $data = $this->haveIssetReceiveData(['token' => ' ', 'pay_password' => ' ', 'old_pay_password' => ' ', 're_pay_password'=>' ']);
        return $this->stepRun($data, 'editSelfPayPassword', 'editSelfPayPassword');
    }
    // 管理员重置普通用户支付密码
    public function resetUserPayPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'pay_password'=>' ', 're_pay_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'resetUserPayPassword', 'resetUserPayPassword');
    }
    // 管理员重置所有用户支付密码
    public function resetAllPayPassword(): array
    {
        $setArr = ['id'=>'', 'name'=>'', 'phone'=>'', 'pay_password'=>' ', 're_pay_password'=>' '];
        $data = $this->haveIssetReceiveData($setArr);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return $this->stepRun($data, 'resetAllPayPassword', 'resetAllPayPassword');
    }

    //用户修改个人基本信息 包括other_info
    public function updateUserInfo(): array
    {
        $data = $this->haveIssetReceiveData(['token' => ' ', 'nickname' => '', 'sex' => '', 'head_portrait'=>'', 'other_info'=>'{}']);
        if($data['other_info'] === '{}' && array_eq($data, '', ['nickname', 'sex', 'head_portrait'])){
            return verify_err($data, 'nickname,sex,other_info,head_portrait至少要传一个');
        }
        if ($data['other_info'] !== '{}' && is_array($data['other_info'])){
            $data['other_info'] = json_encode($data['other_info']);
        }
        return $this->stepRun($data, 'updateUserInfo', 'updateUserInfo');
    }

    //用户进行实名认证
    public function authentication(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ', 'id_number'=>' ', 'front_img'=>' ', 'back_img'=>' ','real_name'=>' ']);
        return $this->stepRun($data, 'authentication', 'authentication');
    }

    /*用户进行换绑手机*/
    public function changePhone(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ','phone'=>' ']);
        return $this->stepRun($data, 'changePhone', 'changePhone');
    }

    /*管理员审核用户的实名认证信息*/
    public function verifyAuthentication(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>0, 'status'=>' '],['']);
        return $this->stepRun($data, 'verifyAuthentication', 'verifyAuthentication');
    }

    /*管理员获取用户的实名认证列表*/
    public function getAuthticationList(): array
    {
        $data = $this->haveIssetReceiveData(['status'=>' ', 'page'=>1, 'list_rows'=>'', 'keywords'=>''],['']);
        return $this->stepRun($data, 'getAuthticationList', 'getAuthticationList');
    }

    /*获取我的账户安全级别*/
    public function getSecurityLevel(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0]);
        return $this->stepRun($data, 'getSecurityLevel');
    }

    //最近一周每天的用户注册量(新增用户) / 平台总用户
    //SELECT COUNT(*) AS counts,FROM_UNIXTIME(create_time,'%Y-%m-%d') AS create_time_date FROM tp_user WHERE create_time>UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 WEEK)) GROUP BY create_time_date    其中WEEK可换成DAY MONTH YEAR
    public function userIncreTendency(): array 
    {
        $data = $this->haveIssetReceiveData(['dbDateFormat'=>'%y-%m-%d', 'dateFormat'=>'y-m-d']);
        return $this->stepRun($data, 'userIncreTendency');
    }
    // 后台添加管理员
    public function addManager(): array 
    {
        $data = $this->haveIssetReceiveData(['name'=>' ', 'nickname'=>'0', 'sex'=>'no', 'phone'=>'0', 'login_password'=>' ', 'other_info' => '{}']);
        if($data['other_info'] !== '{}'){ $data['other_info'] = json_encode($data['other_info']); }
        return $this->stepRun($data, 'addManager', 'register');
    }

}
